# encoding: utf-8
from __future__ import absolute_import, unicode_literals

from rest_framework import serializers

from apps.wechat.models import User

from apps.wechat.serializer import UserSerializer
from core import utils

from . import models


class CourtSerializer(utils.BaseSerializer):
    class Meta:
        model = models.Court
        fields = ('id', 'name', 'description', 'light', 'usage', 'half_no', 'plc_no')


class CourtDetailSerializer(utils.BaseSerializer):
    class Meta:
        model = models.Court
        fields = ('id', 'name', 'description', 'light', 'usage', 'half_no', 'plc_no')


class BookingSerializer(utils.BaseSerializer):
    class Meta:
        model = models.Booking
        fields = ('id', 'user', 'court', 'date', 'start_time', 'end_time')


class BookingDetailSerializer(utils.BaseSerializer):
    user = UserSerializer(many=False, read_only=True)
    court = CourtSerializer(many=False, read_only=True)

    is_manager = serializers.SerializerMethodField("func_is_manager")

    def func_is_manager(self, obj):
        if self.request is None or not isinstance(self.request.user, User):
            return False
        return self.request.user.pk == obj.user_id

    class Meta:
        model = models.Booking
        fields = (
            'id', 'user', 'court', 'date', 'start_time', 'end_time', 'is_manager'
        )
